/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.adv.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.hk.sys.entity.goods.HkGoods;
import com.thinkgem.jeesite.modules.hk.sys.service.goods.HkGoodsService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.adv.entity.HkTraditionAdvReward;
import com.thinkgem.jeesite.modules.hk.adv.service.HkTraditionAdvRewardService;

/**
 * 传统文化配置Controller
 * @author 罗天文
 * @version 2018-03-20
 */
@Controller
@RequestMapping(value = "${adminPath}/adv/hkTraditionAdvReward")
public class HkTraditionAdvRewardController extends BaseController {
	@Autowired
	private HkGoodsService hkGoodsService;
	@Autowired
	private HkTraditionAdvRewardService hkTraditionAdvRewardService;
	
	@ModelAttribute
	public HkTraditionAdvReward get(@RequestParam(required=false) String id) {
		HkTraditionAdvReward entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hkTraditionAdvRewardService.get(id);
		}
		if (entity == null){
			entity = new HkTraditionAdvReward();
		}
		return entity;
	}
	
	@RequiresPermissions("adv:hkTraditionAdvReward:view")
	@RequestMapping(value = {"list", ""})
	public String list(HkTraditionAdvReward hkTraditionAdvReward, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HkTraditionAdvReward> page = hkTraditionAdvRewardService.findPage(new Page<HkTraditionAdvReward>(request, response), hkTraditionAdvReward); 
		model.addAttribute("page", page);
		return "hk/adv/hkTraditionAdvRewardList";
	}

	@RequiresPermissions("adv:hkTraditionAdvReward:view")
	@RequestMapping(value = "form")
	public String form(HkTraditionAdvReward hkTraditionAdvReward, Model model) {
		model.addAttribute("hkTraditionAdvReward", hkTraditionAdvReward);
		model.addAttribute("countrys", UserUtils.getAreaListByParentId("0"));
		if(null!=hkTraditionAdvReward.getProvince()){
			model.addAttribute("provinces",UserUtils.getAreaListByParentId(hkTraditionAdvReward.getCountry().getId()));
		}
		if(null!=hkTraditionAdvReward.getCity()){
			model.addAttribute("citys",UserUtils.getAreaListByParentId(hkTraditionAdvReward.getProvince().getId()));
		}
		model.addAttribute("goods",hkGoodsService.findList(new HkGoods()));
		return "hk/adv/hkTraditionAdvRewardForm";
	}

	@RequiresPermissions("adv:hkTraditionAdvReward:edit")
	@RequestMapping(value = "save")
	public String save(HkTraditionAdvReward hkTraditionAdvReward, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, hkTraditionAdvReward)){
			return form(hkTraditionAdvReward, model);
		}
		hkTraditionAdvRewardService.save(hkTraditionAdvReward);
		addMessage(redirectAttributes, "保存传统文化配置成功");
		return "redirect:"+Global.getAdminPath()+"/adv/hkTraditionAdvReward/?repage";
	}
	
	@RequiresPermissions("adv:hkTraditionAdvReward:edit")
	@RequestMapping(value = "delete")
	public String delete(HkTraditionAdvReward hkTraditionAdvReward, RedirectAttributes redirectAttributes) {
		hkTraditionAdvRewardService.delete(hkTraditionAdvReward);
		addMessage(redirectAttributes, "删除传统文化配置成功");
		return "redirect:"+Global.getAdminPath()+"/adv/hkTraditionAdvReward/?repage";
	}

}